import request from '@/utils/request'

const userApi = {
    Login: '/login',
    LoginByCode: '/sso/oauth/token',
    loginBySsoCode: window.HC_config && window.HC_config.VUE_APP_SSO_LOGIN_URL || '/auth/loginByCode',
    logoutBySsoCode: window.HC_config && window.HC_config.VUE_APP_SSO_LOGOUT_URL ||  '/auth/logoutByCode',
    loginBySsoClientIp: '/auth/loginByClientIp',
    loginBySso: "/loginBySecretKey",
    goBacklogBySecretKey: "/auth/goBacklogBySecretKey",
    Logout: '/personel/auth/logout',
    ForgePassword: '/auth/forge-password',
    Register: '/auth/register',
    twoStepCode: '/auth/2step-code',
    SendSms: '/account/sms',
    SendSmsErr: '/account/sms_err',
    // get my info
    UserInfo: '/system/user/info',
    UserMenu: '/system/user/nav',
    //获取登录验证码
    getLoginQRCode: '/getLoginQRCode',
    //二维码登录（短轮询） 建议修改方案 长轮询
    loginScanCode: '/loginScanCode',
    loginBySsoClientId: '/hzjwsso/loginBySsoToken'
}

/**
 * login func
 * parameter: {
 *     username: '',
 *     password: '',
 *     remember_me: true,
 *     captcha: '12345'
 * }
 * @param parameter
 * @returns {*}
 */
export function login(params)
{
    return request({
        url: userApi.Login,
        method: 'post',
        params,
        noShowError: true, //不提示错误信息
        noLoading: true //不显示加载
    })
}


export function loginByCode(code)
{
    const formData = new FormData()
    formData.append('grant_type', 'authorization_code')
    formData.append('client_id', window.HC_config.VUE_APP_CLIENT_ID)
    formData.append('client_secret', window.HC_config.VUE_APP_CLIENT_ID_SERCET)
    formData.append('code', code)
    formData.append('redirect_uri', window.location.protocol + '//' + window.location.host + process.env.VUE_APP_PUBLIC_PATH + '/user/login')
    return request({
        url: userApi.LoginByCode,
        method: 'post',
        data: formData
    })
}

//获取登录验证码
export function getLoginQRCode()
{
    return request({
        url: userApi.getLoginQRCode,
        method: 'get',
    })
}

//二维码登录（轮询）
export function loginScanCode(parameter)
{
    return request({
        url: userApi.loginScanCode,
        method: 'get',
        params: parameter
    })
}

export function loginBySso(parameter)
{
    return request({
        url: userApi.loginBySso,
        method: 'post',
        params: parameter
    })
}

export function loginByUkey(data)
{
    return request({
        url: '/loginByUkey',
        method: 'post',
        data
    })
}

//短信验证码登录
export function loginByMsg(params)
{
    return request({
        url: "/loginByMsg",
        method: "post",
        params,
        noShowError: true, //不提示错误信息
        noLoading: true //不显示加载
    })
}


//短信验证码登录
export function resetPwdByToken(params)
{
    return request({
        url: "/resetPwdByToken",
        method: "post",
        params,
        noShowError: true, //不提示错误信息
        noLoading: true //不显示加载
    })
}

export function loginBySsoToken(data)
{
    let apiUrl = window.HC_config.VUE_APP_SSOAPIULR || "/Sso/loginBySsoToken";
    return request({
        url: apiUrl,
        method: "post",
        data
    })
}


export function goBacklogBySecretKey(parameter)
{
    return request({
        url: userApi.goBacklogBySecretKey,
        method: 'get',
        params: parameter
    })
}

export function loginBySsoCode(parameter)
{
    return request({
        url: userApi.loginBySsoCode,
        method: 'get',
        params: parameter
    })
}

export function logoutBySsoCode(parameter)
{
    return request({
        url: userApi.logoutBySsoCode,
        method: 'POST',
        params: parameter
    })
}

export function loginBySsoClientIp(parameter)
{
    return request({
        url: userApi.loginBySsoClientIp,
        method: 'get',
        params: parameter
    })
}

export function loginBySsoClientId(parameter)
{
    return request({
        url: userApi.loginBySsoClientId,
        method: 'get',
        params: parameter
    })
}

export function getSmsCaptcha(parameter)
{
    return request({
        url: userApi.SendSms,
        method: 'post',
        data: parameter
    })
}

export function getInfo()
{
    return request({
        url: userApi.UserInfo,
        method: 'get'
    })
}

export function getSystemInfo()
{
    return request({
        url: "/system/person/getSystemInfo",
        method: 'get'
    })
}


export function getCurrentUserNav(data)
{
    return request({
        url: userApi.UserMenu,
        method: 'get',
        params: data
    })
}

export function logout(data)
{
    return request({
        url: userApi.Logout,
        method: 'post',
        data,
        noShowError: true
    })
}

/**
 * get user 2step code open?
 * @param parameter {*}
 */
export function get2step(parameter)
{
    return request({
        url: userApi.twoStepCode,
        method: 'post',
        data: parameter
    })
}
